# -*- coding: utf-8 -*
# 数据操作控制器
#!/usr/bin/python3


import sys
import os
sys.path.append(os.getcwd() + "\\2048")
from Core.Data import GameData
from enum import Enum


class Operation(Enum):
    Up = 1,
    Down = 2,
    Left = 3,
    Right = 4,


class InternalMunber:
    key_data = "data"
    key_callBack = "call_back"

class OperationController:

    __internal_mumber = None
   

    # 数据操纵
    # @opt_enum 是一个操作枚举类型 Operation
    # @return nil
    @staticmethod
    def Opt(opt_enum):
        if opt_enum not in Operation:
            print("error opt_enum is error!  opt_enum" , opt_enum)
            return
        return OperationController.__Selection(opt_enum)

    

    # 根据操作类型排列数据
    # @data 从GameData中按照操作类型重新组合数据 key值调用 GetSelectionDataKey() 方法获取
    # @call_back 按选择顺序还原data到GameData中 key值调用 GetSelectionCallBackKey() 方法获取
    @staticmethod
    def __Selection(opt_enum):
        lists = None
        call_back = None
        if opt_enum == Operation.Up:
            lists = OperationController.__SelectDateOrderByUp()
            call_back = OperationController.__SelectDateOrderByUpCallBack

        elif opt_enum == Operation.Down:
            lists = OperationController.__SelectDateOrderByDown()
            call_back = OperationController.__SelectDateOrderByDownCallBack

        elif opt_enum == Operation.Left:
            lists = OperationController.__SelectDateOrderByLeft()
            call_back = OperationController.__SelectDateOrderByLeftCallBack

        elif opt_enum == Operation.Right:
            lists = OperationController.__SelectDateOrderByRight()
            call_back = OperationController.__SelectDateOrderByRightCallBack

        dirc = {}
        global __internal_mumber
        dirc[__internal_mumber.key_data] = lists
        dirc[__internal_mumber.key_callBack] = call_back


        return dirc


    #获取Selection返回字典中data的key
    @staticmethod
    def GetSelectionDataKey():
        global __internal_mumber
        return __internal_mumber.key_data

    #获取Selection返回字典中callBackFunc的key
    @staticmethod
    def GetSelectionCallBackKey():
        global __internal_mumber
        return __internal_mumber.key_callBack

    
    # 列转行 + 反转
    @staticmethod
    def __SelectDateOrderByUp():
        lists = OperationController.__SelectDateOrderByDown()
        for index in iter(range(len(lists))):
            lists[index].reverse()
        return lists
    
    @staticmethod
    def __SelectDateOrderByUpCallBack(arg_lists):
        for index in iter(range(len(arg_lists))):
            arg_lists[index].reverse()
        
        arg_lists = OperationController.__SelectDateOrderByDownCallBack(arg_lists)
        return arg_lists



    # 列转行
    @staticmethod
    def __SelectDateOrderByDown():
        # up 是列转行
        lists = []
        data = GameData.GameData.GetGameData()
        for col in iter(range(len(data))):
            temp_list = []
            for row in iter(range(len(data[0]))):
                temp_list.append(data[row][col])
            lists.append(temp_list)

        return lists
 
    @staticmethod
    def __SelectDateOrderByDownCallBack(arg_lists):
        # 行转列
        lists = []
        data = arg_lists
        for row in iter(range(len(data))):
            temp_list = []
            for col in iter(range(len(data[0]))):
                temp_list.append(data[col][row])
            lists.append(temp_list)
        return lists





    
    # 反转
    @staticmethod
    def __SelectDateOrderByLeft():
        lists = OperationController.__SelectDateOrderByRight()
        print("reverse before " , lists)
        for index in iter(range(len(lists))):
            lists[index].reverse()
        print("reverse after " , lists)
        return lists

    @staticmethod
    def __SelectDateOrderByLeftCallBack(arg_lists):
        print("__SelectDateOrderByRightCallBack"   , arg_lists)
        for index in iter(range(len(arg_lists))):
            arg_lists[index].reverse()
        
        arg_lists = OperationController.__SelectDateOrderByRightCallBack(arg_lists)
        return arg_lists
            



    # 不需要转
    @staticmethod
    def __SelectDateOrderByRight():
         return list(GameData.GameData.GetGameData())
        


    @staticmethod
    def __SelectDateOrderByRightCallBack(arg_lists):
        return list(arg_lists)


    @staticmethod
    def InitFunc():
        global __internal_mumber
        __internal_mumber = InternalMunber()

    @staticmethod
    def Print():
        global __internal_mumber
        print(__internal_mumber.key_data)
        print(__internal_mumber.key_callBack)


OperationController.InitFunc()
# OperationController.Print()